App({
    onLaunch() {},

    // 接受 index.js 传递过来的data对象, watch对象
    setWatcher(page) {
        /**
                         *     
                            data: {
                                my: {
                                    name: "xuyang",
                                    age: 21,
                                    hobby: ["girls", "games"],
                                },
                                nameInfo: {},
                            },
                         */
        let data = page.data;
        /**
                         *     
                            watch: {
                                my: {
                                    handler(newValue) {
                                        console.log("数据更新了", newValue);
                                    },
                                    deep: true,
                                },
                            },
                         */
        let watch = page.watch;
        // 遍历watch对象内的key

        console.log("Object.keys(watch):     ", Object.keys(watch));

        Object.keys(watch).forEach((v) => {
            // v => my
            // key => ["my"]
            let key = v.split(".");
            // {my: {…}, nameInfo: {…}, __webviewId__: 15}
            let nowData = data;

            for (let i = 0; i < key.length - 1; i++) {
                nowData = nowData[key[i]];
                console.log("循环内");

                console.log(nowData);
            }

            // lastKey: my
            let lastKey = key[key.length - 1];
            console.log(lastKey);

            let watchFun = watch[v].handler || watch[v];
            console.log(watchFun);

            let deep = watch[v].deep;

            this.observe(nowData, lastKey, watchFun, deep, page);
        });
    },

    /**
     * 监听属性, 并执行监听函数
     */
    observe(obj, key, watchFun, deep, page) {
        // val => {name: "xuyang", age: 21, hobby: ["girls", "games"]}
        var val = obj[key]; // 给该属性设默认值
        // 判断deep是true 且 val不能为空 且 typeof val==='object'（数组内数值变化也需要深度监听）
        if (deep && val !== null && typeof val === "object") {
            console.log(typeof val);

            /**
             * 第二次调用observe函数
             * Object.keys(val) => ["name", "age", "hobby"]
             */
            Object.keys(val).forEach((childKey) => {
                console.log('childKey: ', childKey);

                this.observe(val, childKey, watchFun, deep, page);
            });
        }
        var that = this;

        Object.defineProperty(obj, key, {
            configurable: true,
            enumerable: true,
            set: function(value) {
                console.log("执行set函数", value);

                watchFun.call(page, value, val);
                val = value;

                if (deep) {
                    that.observe(obj, key, watchFun, deep, page);
                }
            },
            get: function() {
                return val;
            },
        });
    },

    globalData: {},
});